package com.didibigdata.Repositories;

import com.didibigdata.entities.ODData;
import com.didibigdata.entities.OrderSave;
import com.didibigdata.utils.JDBCTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by lkabb on 2020/6/7
 */
public class ODDataRepository {
    public List<ODData> getODDataList() {
        //日志
        Logger logger = LoggerFactory.getLogger(ODDataRepository.class);
        logger.info("add WorkTimeRepository logger successful!");

        ResultSet resultSetmysql = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<OrderSave> orderSaveList = new ArrayList<>();
        List<ODData> odDataList = new ArrayList<>();
        try {
            connectionmysql = JDBCTools.getConnection();
            String sqltime = "select * from t_didi201610gps_points_in_out";
            statementmysql = connectionmysql.prepareStatement(sqltime);
            resultSetmysql = statementmysql.executeQuery();
            Set<String> orderSet = new HashSet<String>();
            while(resultSetmysql.next()) {
                OrderSave oSave = new OrderSave();
                oSave.setOrderId(resultSetmysql.getString(3));
                oSave.setPath(resultSetmysql.getString(8));
                orderSet.add(resultSetmysql.getString(8));
                orderSaveList.add(oSave);
            }
            for (String eachOrderSet : orderSet) {
                int num = 0;
                ODData odd = new ODData();
                resultSetmysql.beforeFirst();
                for (OrderSave osl : orderSaveList) {
                    if(eachOrderSet.equals(osl.getPath())) {
                        num++;
                    }
                }
                odd.setPathId(eachOrderSet);
                odd.setPathDataNum(num);
                odDataList.add(odd);
            }
            for (ODData odData : odDataList) {
                logger.info("pathId: [{}] and pathDataNum: [{}]", odData.getPathId(), odData.getPathDataNum());
            }
        }catch (Exception e) {
            logger.error("ODDataRepository SQLException: [{}]", e);
            e.printStackTrace();
        }finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resultSetmysql);
        }

        return odDataList;
    }
}
